<?php

namespace app\admin\controller;

use app\admin\controller\Base;
use think\Request;
use app\admin\model\Role;
use app\admin\model\User;
use app\admin\model\User_role;
use app\admin\model\Role_node;


class AuthRole extends Base
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $where = function($query){
            $query->field('password',true);
        };
        $list = User::all($where);

        $data = [];
        foreach ($list as $val){
            $id = $val->getData()['id'];
            $data[$id] = $val->getData();
            $rid = User_role::allRid($id);
            $data[$id]['role'] = Role::allRole($rid);
        }

        $node = Role::all();
        $nodes = [];
        foreach ($node as $val){
            $nodes[] = $val->getData();
        }

        return view('user/auth', ['data'=>$data, 'nodes'=>$nodes]);
    }

    // /**
    //  * 显示创建资源表单页.
    //  *
    //  * @return \think\Response
    //  */
    // public function create()
    // {
    //     $list = Role::all();
    //     $data = [];
    //     foreach ($list as $val){
    //         $data[] = $val->getData();
    //     }
    //     return view('user/auth', ['data'=>$data]);
    // }



    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $info = input('put.');
        User_role::where('uid', '=', $id)->delete();
        $result = 1;
        if (count($info ) > 1){
            $user_role = new User_role();
            $result = $user_role->add($info['role'],$id);
        }
        $data = [];
        //判断
        if ($result>0){
            $data['status'] = true;
            $data['message'] = '添加成功';
        }else{
            $data['status'] = false;
            $data['message'] = '添加失败';
        }
        return json($data);
    }

}
